import java.util.concurrent.atomic.AtomicReference;

public class SpinlockDemo {
    // int 0
// Thread null
    AtomicReference<Thread> atomicReference = new AtomicReference<>();
    // 加锁
    public void myLock(){
        Thread thread = Thread.currentThread();
        System.out.println(Thread.currentThread().getName() + "==> mylock");
        // 自旋锁
        //期望为null，就设置为thread
        while (!atomicReference.compareAndSet(null,thread)){
        }
    }
    // 解锁
    public void myUnLock(){
        Thread thread = Thread.currentThread();
        System.out.println(Thread.currentThread().getName() + "==> myUnlock");
        //期望为thread，就设置为null
        atomicReference.compareAndSet(thread,null);
    }
}
